package com.jintoufs.service.tour.impl;

import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.tour.TourRouteOrderDetailMapper;
import com.jintoufs.dao.tour.TourRouteOrderMapper;
import com.jintoufs.dao.travelagency.TravelAgencyMapper;
import com.jintoufs.domain.account.AccountDetail;
import com.jintoufs.domain.constants.Constants;
import com.jintoufs.domain.tour.TourRoute;
import com.jintoufs.domain.tour.TourRouteOrder;
import com.jintoufs.domain.tour.TourRouteOrderDetail;
import com.jintoufs.service.account.AccountDetailService;
import com.jintoufs.service.shop.ShopService;
import com.jintoufs.service.sys.DictionaryService;
import com.jintoufs.service.tour.TourRouteOrderService;
import com.jintoufs.service.tour.TourRouteService;
import com.jintoufs.service.weixin.WxRefundService;
import com.jintoufs.util.valid.ValidUtil;
import com.jintoufs.uuid.SnowflakeIdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class TourRouteOrderServiceImpl implements TourRouteOrderService {
    @Resource
    private AccountDetailService accountDetailService;

    @Resource
    private TourRouteOrderDetailMapper tourRouteOrderDetailMapper;

    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;

    @Resource
    private TourRouteOrderMapper tourRouteOrderMapper;

    @Resource
    private ShopService shopService;
    @Resource
    private WxRefundService refundService;
    @Resource
    private DictionaryService dictionaryService;

    @Resource
    private TourRouteService tourRouteService;

    @Resource
    private TravelAgencyMapper travelAgencyMapper;

    @Override
    public List<Map<String, Object>> queryByParams(Map<String, Object> params) {
        return tourRouteOrderMapper.queryByParams(params);
    }

    @Override
    public int payConfirm(String orderId) {
        TourRouteOrder tourRouteOrder = tourRouteOrderMapper.selectByPrimaryKey(orderId);
      //  TourRoute tourRoute = tourRouteService.getRouteById(tourRouteOrder.getTourRouteId());
        tourRouteOrder.setStatus(Constants.ORDER_STATUS_CONFIRM);
        tourRouteOrder.setReadStatus(Constants.ORDER_READ_STATUS_0);
        tourRouteOrder.setModifyTime(new Date());
        tourRouteOrder.setIsNew(Constants.ORDER_IS_NEW);
        tourRouteOrderMapper.updateByPrimaryKeySelective(tourRouteOrder);

        Map<String,Object> params = new HashMap<>();
        params.put("id",orderId);
        List<Map<String,Object>> list =  queryByParams(params);
        if (ValidUtil.isEmpty(list)){
            return 1;
        }
        Integer smsId;
        ArrayList<String> smsParams = new ArrayList<>();

        smsId = Integer.valueOf(dictionaryService.getDicByKeyNo("ROUTE_CONFIRM_SMS").get(0).getCaption());
        if (!ValidUtil.isEmpty(list)){
            Object object;

            object = list.get(0).get("start_date");
            if (ValidUtil.isEmpty(object)){
                object="";
            }

            smsParams.add(object.toString());
            smsParams.add(list.get(0).get("routeName").toString());

            object = list.get(0).get("link_phone");
            if (!ValidUtil.isEmpty(object)){
              List<TourRouteOrderDetail> tourRouteOrderDetails=  tourRouteOrderDetailMapper.selectByRouteOrderId(tourRouteOrder.getId());
                if(tourRouteOrderDetails!=null&&tourRouteOrderDetails.size()>0){
                    String customerPhone=tourRouteOrder.getContactTel();
                    if(!ValidUtil.isEmpty(customerPhone)){
                        String telephone = list.get(0).get("link_phone").toString();
                        smsParams.add(telephone);
                        refundService.sendOrderSms(customerPhone,smsId,smsParams);
                    }
                }
            }

        }
        return 1;
    }
    public String getUUID(){
        return String.valueOf(snowflakeIdWorker.nextId());
    }

    @Override
    public List<Map<String,Object>> getOrderList(Map<String,Object> param) {
        return tourRouteOrderMapper.getOrderInfo(param);
    }

    @Override
    public TourRouteOrder update(TourRouteOrder tourRouteOrder) {
        tourRouteOrder.setIsNew(Constants.ORDER_IS_NEW);
        tourRouteOrderMapper.updateByPrimaryKeySelective(tourRouteOrder);
        return tourRouteOrder;
    }

    @Override
    public Map getOrderPriceAndCostByOrderId(String orderId) {
        return tourRouteOrderMapper.getOrderPriceAndCostByOrderId(orderId);
    }

    @Override
    public TourRouteOrder getById(String id) {
        return tourRouteOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public Map<String, Object> selectById(String id) {
        Map<String,Object> params = new HashMap<>();
        params.put("id",id);
        return tourRouteOrderMapper.getOrderInfo(params).get(0);
    }

    @Override
    public List<TourRouteOrderDetail> selectDetailByRouteOrderId(String id) {
        return tourRouteOrderDetailMapper.selectByRouteOrderId(id);
    }

    @Override
    public List<Map<String, Object>> queryOrderByFilters(QueryFilters filters) {
        return tourRouteOrderMapper.queryOrderByFilters(filters);
    }

    @Override
    public List<TourRouteOrder> getNoConfirm(Map<String, Object> param) {
        return tourRouteOrderMapper.getNoConfirm(param);
    }

    @Override
    public int updateOrderNew(Map param) {
        return tourRouteOrderMapper.updateOrderNew(param);
    }
}
